﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Collections;
using System.Net.Mail;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ACP.Business;
using ACP.DTO;

namespace ACP.Web.UI
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           // divContactSuccess.Visible = false;
            //footer_top_inner_in_left.Visible = true;
            
        }


        protected void SubmitBtn_Click(object sender, EventArgs e)
        {
            try
            {
                EnquiryInfo enquiryInfo = new EnquiryInfo()
                {
                    SupportTypeID = 4,
                    FullName = txtFullName.Text.Trim(),
                    Email = txtEmail.Text.Trim(),
                    Mobile = string.Empty,
                    AltPhone = string.Empty,
                    City = string.Empty,
                    State = string.Empty,
                    Location = string.Empty,
                    PreferredLocation = string.Empty,
                    Aligibility = string.Empty,
                    Designation = string.Empty,
                    Company = string.Empty,
                    Instructions = messageBox.Text.Trim()
                };

                SupportLogic.InsertSupportInformation(enquiryInfo);
                SendMail();
                //divContactSuccess.Visible = true;
                //footer_top_inner_in_left.Visible = false;
                SuccessMsg.Visible = true;
                txtEmail.Text = "";
                txtFullName.Text = "";
                messageBox.Text = "";
                txtFullName.Focus();
            }
            catch (Exception ex)
            {
                acptest_ErrorLog errorLog = new acptest_ErrorLog()
                {
                    ErrorMessage = ex.Message,
                    ErrorFunction = "SubmitBtn_Click",
                    InsertedTime = DateTime.Now,
                    ModuleName = "Default.aspx"
                };
                ACPErrorLogLogic.ErrorLog(errorLog);
            }
        }

        private void SendMail()
        {
            try
            {
               
                int _mailPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SMTPPort"]);
                string _mailServer = ConfigurationManager.AppSettings["MailServer"];
                string fromAddress = ConfigurationManager.AppSettings["from"];
                string toAddress = ConfigurationManager.AppSettings["toAddress"];
                string smtpUserName = ConfigurationManager.AppSettings["username"];
                string smtpPassword = ConfigurationManager.AppSettings["password"];

                SmtpClient smtpClient = new SmtpClient(_mailServer);

                smtpClient.Credentials = new System.Net.NetworkCredential(smtpUserName, smtpPassword);

                // Default in IIS will be localhost 
                //smtpClient.Host = "localhost";

                //Default port will be 25
                smtpClient.Port = _mailPort;


                MailMessage message = new MailMessage();
                message.IsBodyHtml = false;
                message.Priority = MailPriority.High;
                message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

                try
                {
                    message.Subject = "Contact Us";
                    message.Body = "Sender: " + txtFullName.Text.Trim() + "\n";
                    //message.Body += "Street: " + streetBox.Text.Trim() + "\n";
                    message.Body += "Email: " + txtEmail.Text.Trim() + "\n";
                    message.Body += "\n\n" + this.messageBox.Text + "\n\n";

                    smtpClient.Send(fromAddress, toAddress, message.Subject, message.Body);
                }
                catch (Exception ex)
                {
                    // Display error panel
                    //contactLabel.Visible = false;
                    //formPanel.Visible = false;
                    //errorPanel.Visible = true;

                    // Log error
                    //LogError(ex);
                }
                //contactLabel.Visible = false;
                //formPanel.Visible = false;
                //errorPanel.Visible = false;
                //successPanel.Visible = true;
            }
            catch (Exception ex)
            {
                acptest_ErrorLog errorLog = new acptest_ErrorLog()
                {
                    ErrorMessage = ex.Message,
                    ErrorFunction = "SendMail()",
                    InsertedTime = DateTime.Now,
                    ModuleName = "Enquiry.aspx"
                };
                ACPErrorLogLogic.ErrorLog(errorLog);
            }

        }

    }
}
